//
//  QueryController.swift
//  Biubiubiu
//
//  Created by DevWhan on 2019/5/21.
//  Copyright © 2019 DevWhan. All rights reserved.
//

import Cocoa

class QueryController: NSWindowController {

    @IBOutlet weak var name: NSTextField!
    @IBOutlet weak var query: NSTextField!
    
    var targetQuery: TargetFile?
    var confirmBlock: ((_ name: String,_ query: String) -> Void)?
    
    
    override func windowDidLoad() {
        super.windowDidLoad()

        if let fileName = targetQuery?.path.fileName{
            self.window?.title = fileName
        }
        
        if let query = targetQuery?.query{
            self.query.stringValue = query
        }
        if let name = targetQuery?.name{
            self.name.stringValue = name
        }
        
        self.name.target = self
        self.name.action = #selector(QueryController.confirmClick(_:))
        self.query.target = self
        self.query.action = #selector(QueryController.confirmClick(_:))
    }
    
    @IBAction func confirmClick(_ sender: NSButton) {
        if let confirmBlock = self.confirmBlock {
            confirmBlock(self.name.stringValue,self.query.stringValue)
        }
        window?.performClose(self)
    }
}
